import {reqAddUrUpdateCart, reqDetailList} from "@/api";

//detail模块的小仓库
let state = {
     // 商品详情的数据
     detailInfo:{}
};
let mutations = {
     GETDETAILINFO(state,detailInfo){
          state.detailInfo = detailInfo;
     }
     
};
let actions = {
     async getDetailInfo({commit,dispatch,state},skuId){
          let result = await reqDetailList(skuId);
          if(result.code == 200){
               commit('GETDETAILINFO',result.data)
          }
     },
     // 加入购物车、修改商品个数
     async addOrUpdateCart({commit,dispatch,state},{skuId,skuNum}){
          let result = await reqAddUrUpdateCart(skuId,skuNum);
          if(result.code == 200){
               return 'ok';
          }else{
               return Promise.reject()
          }

     }
     

};
// 为了简化数据  组件获取数据更方便
let getters = {
     // 面包屑的数据
     categoryView(state){
          return state.detailInfo.categoryView || [];
     },
     // 商品信息的数据
     skuInfo(state){
          return state.detailInfo.skuInfo || {};
     },
     // 商品销售属性列表的数据
     spuSaleAttrList(){
          return state.detailInfo.spuSaleAttrList || {};
     }
};

//对外暴露
export default {
     state,
     mutations,
     actions,
     getters
}